﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using OneSoft.EntityCore;
using OneSoft.DataAccess;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Schema;
using BySoft.Server.DataAccess;

namespace BySoft.Server.Business
{
    public partial class SysFormaudit
    {
        public static DataTable SelectList(string strModuleID,string strIsCheck,
                                                      int pageIndex,
                                                      int pageSize,
                                                      out int count)
        {
            return SysFormauditDA.SelectList(strModuleID,strIsCheck,pageIndex, pageSize, out count);
        }

        public static DataTable SelectHead(string strCode)
        {
            return SysFormauditDA.SelectHead(strCode);
        }

        public static DataTable SelectBody(string strCode)
        {
            return SysFormauditDA.SelectBody(strCode);
        }

        public static DataTable SelectBody(string strCode, DataAccessBroker broker)
        {
            return SysFormauditDA.SelectBody(strCode, broker);
        }

        public static string CheckUsed(string strCode)
        {
            string str = string.Empty;
            DataTable dt = SysFormauditDA.CheckUsed(strCode);
            foreach (DataRow row in dt.Rows)
            {
                if (!string.IsNullOrEmpty(row["tablename"].ToString().Trim()))
                {
                    str += row["tablename"].ToString() + ",";
                }
            }
            return str.Trim();
        }

        public static void SaveOrUpdate(SysFormauditEntity Entity, string Type, List<SysFormauditdetailEntity> lstbody)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                broker.BeginTransaction();
                try
                {
                    if (Type == "Save")
                    {
                        DataAccessCommon.Insert(Entity, broker);
                        for (int i = 0; i < lstbody.Count; i++)
                        {
                            lstbody[i].FormAuditID= Entity.FormAuditID;
                            DataAccessCommon.Insert(lstbody[i], true, broker);
                        }
                    }
                    else
                    {
                        DataTable dt = SysFormaudit.SelectBody(Entity.FormAuditID, broker);
                        for (int j = 0; j < dt.Rows.Count; j++)
                        {
                            int isExist = 0;
                            for (int i = 0; i < lstbody.Count; i++)
                            {
                                if (lstbody[i].FormAuditDetailID.ToString() == dt.Rows[j]["FormAuditDetailID"].ToString())
                                {
                                    isExist = 1;
                                }
                            }
                            if (isExist == 0)
                            {
                                ClsCommDA.Del("SYS_FormAuditDetail", "FormAuditDetailID", dt.Rows[j]["FormAuditDetailID"].ToString(), broker);
                            }
                        }

                        for (int i = 0; i < lstbody.Count; i++)
                        {
                            if (lstbody[i].FormAuditDetailID.ToString() != "-1")
                            {
                                DataAccessCommon.UpdateOnly(lstbody[i], broker);
                            }
                            else
                            {
                                lstbody[i].FormAuditID= Entity.FormAuditID;
                                DataAccessCommon.Insert(lstbody[i], true, broker);
                            }
                        }
                        DataAccessCommon.UpdateOnly(Entity, broker);
                    }
                    broker.Commit();
                }
                catch
                {
                    broker.Rollback();
                    throw;
                }
            }
        }
        
        public static void UpdateStatus(string strCode)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    ClsCommDA.UpdateStatus("SYS_FormAudit", "FormAuditID", strCode, broker);
                    broker.Commit();
                }
                catch
                {
                    broker.Rollback();
                    throw;
                }
            }
        }

		public static void Del(string strCode)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
					ClsCommDA.Del("SYS_FormAuditDetail", "FormAuditID", strCode, broker);
                    ClsCommDA.Del("SYS_FormAudit", "FormAuditID", strCode, broker);
                    broker.Commit();
                }
                catch
                {
                    broker.Rollback();
                    throw;
                }
            }
        }
    }
}

